package com.ruoyi.system.service.module.product.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.Aes;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.OssUtil;
import com.ruoyi.common.utils.QRCodeUtilEx;
import com.ruoyi.system.domain.module.product.Product;
import com.ruoyi.system.domain.module.product.ProductAttrValue;
import com.ruoyi.system.domain.module.product.ProductOrder;
import com.ruoyi.system.domain.module.product.ProductOrderInfo;
import com.ruoyi.system.mapper.module.product.ProductAttrValueMapper;
import com.ruoyi.system.mapper.module.product.ProductMapper;
import com.ruoyi.system.mapper.module.product.ProductOrderInfoMapper;
import com.ruoyi.system.mapper.module.product.ProductOrderMapper;
import com.ruoyi.system.service.module.product.IProductOrderInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单详情内容  订单里面的 商品列Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Service
public class ProductOrderInfoServiceImpl extends ServiceImpl<ProductOrderInfoMapper, ProductOrderInfo> implements IProductOrderInfoService {

    @Resource
    private ProductOrderInfoMapper productOrderInfoMapper;
	@Resource
	private ProductAttrValueMapper productAttrValueMapper;
	@Resource
	private ProductMapper productMapper;
	@Resource
	private ProductOrderMapper orderMapper;


	@Override
	public int resetOrderTotalMoney(ProductOrderInfo orderInfo) {
		int ret = 0;
		ProductOrder productOrder = orderMapper.selectById(orderInfo.getOrderId());
		if(productOrder != null){
			double totalMoney = 0;
			QueryWrapper<ProductOrderInfo> qw = new QueryWrapper<>();
			qw.eq("order_id",orderInfo.getOrderId());
			qw.eq("is_del", 0);
			List<ProductOrderInfo> orderInfos = productOrderInfoMapper.selectList(qw);
			if(CollUtil.isNotEmpty(orderInfos)){
				for (ProductOrderInfo orderInfo1 : orderInfos){
					totalMoney += orderInfo1.getSinglePrice().doubleValue() * orderInfo1.getCount();
				}
			}
			productOrder.setTotalPrice(new BigDecimal(totalMoney));
            productOrder.setPayPrice(new BigDecimal(totalMoney));
			ret = orderMapper.updateById(productOrder);
		}
		return ret;
	}

	@Override
	public int getProductCountByInfo(Integer merId,Long productId,String beginTime,String endTime) {
		Map<String,Object> map = new HashMap<>();
		map.put("mer_id",merId);
		map.put("product_id",productId);
		if(StrUtil.isNotEmpty(beginTime) && !beginTime.equals("null")  && StrUtil.isNotEmpty(endTime)&& !endTime.equals("null") ){
			map.put("begin_time",beginTime + " 00:00:00");
			map.put("end_time",endTime + " 23:59:59");
		}
		return productOrderInfoMapper.getProductCountByInfo(map);
	}


	@Override
    public List<ProductOrder> selectOrderListByMer(Page<ProductOrder> page, Map<String, Object> map) {
        return productOrderInfoMapper.selectOrderListByMer(page,map);
    }

	@Override
	public List<ProductOrderInfo> selectListByOrderId(Long order_id) {
		QueryWrapper<ProductOrderInfo> qw = new QueryWrapper<>();
		qw.eq("order_id", order_id);
		qw.eq("is_del", 0);
		List<ProductOrderInfo> infos = productOrderInfoMapper.selectList(qw);
		if(infos != null && infos.size() > 0) {
			for (ProductOrderInfo orderInfo : infos) {
				if(StrUtil.isEmpty(orderInfo.getSkuName())){
					ProductAttrValue attrValue = productAttrValueMapper.selectById(orderInfo.getSkuId());
					if(attrValue != null){
						orderInfo.setSkuName(attrValue.getSku());
					}
				}

				Product product = productMapper.selectById(orderInfo.getProductId());
				if (product != null) {
					orderInfo.setProductName(product.getStoreName());
				}
			}
		}
		return infos;
	}
}
